package interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class AdminInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		
		// 获取请求的URL
		String url = request.getRequestURI();
		//放行
		if (url.contains("/login/Page") || url.contains("/login/login")|| url.contains("/css")|| url.contains("/image")) {
			return true;
		}

		// 获取Session
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("loginNow");
		if (obj != null)
			return true;
		// 没有登录且不是登录页面，转发到登录页面，并给出提示错误信息
		request.setAttribute("msg", "还没登录，请先登录！");
		request.getRequestDispatcher("/index.jsp").forward(request, response);
		
		return false;
	}

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
